package com.example.model.controller;

import com.example.model.com.entity.Users;
import com.example.model.dao.UserDao;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;


/**
 * Servlet implementation class UserAd
 */
public class UserAddServlet extends HttpServlet {


    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        String userName, password, sex, email;
        UserDao dao = new UserDao();
        Users user = null;
        int result = 0;
        PrintWriter out = null;
        //1.[调用请求对象]读取【请求头】参数信息，得到用户的信息
        userName = request.getParameter("userName");
        password = request.getParameter("password");
        sex = request.getParameter("sex");
//		System.out.println(sex);
        email = request.getParameter("email");
        //2.调用【UserDAO】将用户信息填充到insert命令并借助jdbc规范发送到数据库服务器
        user = new Users(0, userName, password, sex, email);
        result = dao.add(user);
        System.out.println(result);
        //3.【调用相应对象】将处理结果以二进制的形式写入到响应体内

        out = response.getWriter();
        if (result == 1) {
            out.println("<font style='color:red;font-size:40px'>用户信息注册成功</font>");
        } else {
            out.println("<font style='color:red;font-size:40px'>用户信息注册失败</font>");
        }
        //Tomcat负责销毁请求对象和相应对象
        //Tomcat负责将HTTP相应协议包推送到发送请求的浏览器上
        //浏览器根据响应头Content-type指定编译器对响应体二进制内容编辑
        //浏览器将编辑后的结果在窗口中展示给用户【结束】

    }


}
